package cn.itcast.web.controller.system;

import cn.itcast.domain.compony.Company;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.company.CompanyService;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("system/user")
public class UserController extends BaseController{

    @Autowired
    UserService userService;

    @Autowired
    DeptService deptService;

    @Autowired
    RoleService roleService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    AmqpTemplate amqpTemplate;

    @RequestMapping(value = "list",name = "查询所有用户")
    public String findAll(
            @RequestParam(defaultValue = "1")int page,
            @RequestParam(defaultValue = "10")int size){

        //String companyId = "1";

        PageInfo<User> pageInfo = userService.findByPage(page, size, getCompanyId());
        request.setAttribute("page",pageInfo);

        return "system/user/user-list";
    }

    @RequestMapping(value = "toAdd",name = "跳转到添加用户页面")
    public String toAdd(){
        //String companyId = "1";
        //查询当前企业中的所有部门

        List<Dept> list = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }
    @RequestMapping(value = "toAddAdmin",name = "跳转到添加企业系统管理员用户页面")

    public String toAddAdmin(String companyId){
        //String companyId = "1";
        //查询当前企业中的所有部门
        List<Dept> list = deptService.findAll(companyId);
        request.setAttribute("deptList",list);
        return "system/user/user-add";
    }


    @RequestMapping(value = "edit",name = "添加或更新用户")
    public String edit(User user){
        //String companyId = "1";
        //String companyName = "传智播客教育股份有限公司";
        System.out.println(getCompanyId());

        if (user.getCompanyId()==""){
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
        }





        //判断添加还是更新
        if (StringUtils.isBlank(user.getId())) {
            String pwd = user.getPassword();


            userService.save(user);

            //- 创建一个map集合
            HashMap<String, String> map = new HashMap<>();

            //- map中存放收件人邮箱,主题,内容
            map.put("emailTo",user.getEmail());
            map.put("emailSubject","欢迎加入saas大家庭");
            map.put("emailContent","尊敬的 "+user.getUserName()+" 你好:欢迎加入saas大家庭!saas系统的" +
                    "访问路径为 <a href='http://localhost'>http://localhost</a> ,用户名为: "+user.getEmail()+" ," +
                    "密码为: "+pwd);

            //- 获取amqp对象
            //- 发送map到中间件中
            amqpTemplate.convertAndSend("spring_exchange_direct","mail.send",map);
        }else {
            userService.update(user);
        }

        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "toUpdate",name = "跳转到编辑用户页面")
    public String toUpdate(String id){
        //String companyId = "1";
        //查询当前企业中的所有部门
        List<Dept> list = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",list);

        //查询用户信息,放入域中
        User user = userService.findById(id);
        request.setAttribute("user",user);
        return "system/user/user-update";
    }

    @RequestMapping(value = "delete",name = "删除用户")
    public String delete(String id){
        userService.deleteById(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value="roleList",name = "进入角色分配页面")
    public String roleList(String id){
        //- 调用userService,获取用户的信息
        User user = userService.findById(id);
        request.setAttribute("user",user);

        //- 调用roleservice,获取当前企业的所有角色列表
        List<Role> list = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",list);

        //- 调用userService.findRolesByUid,获取用户拥有的角色列表
        List<String> roleIdList = userService.findRolesByUid(id);
        request.setAttribute("userRoleStr", roleIdList.toString());//[12-qw,23-we]

        return "system/user/user-role";
    }

    @RequestMapping(value = "changeRole",name = "分配角色")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);
        return "redirect:/system/user/list.do";
    }
}
